package com.happystreet.info
{
	import com.adobe.serialization.json.JSON;

	public class GoodInfo
	{
		public static const SIMPLE_TYPE:String = "simple";
		public static const UPGRADE_TYPE:String = "upgrade";
		public static const MATERIAL_CURRENCY:String = "material";
		
		private var _baseXml:XML;
		private var _level:int;
		private var _symbolName:String;
		private var _simpleName:String;
		private var _simpleQuantity:int;
		private var _simpleCost:int;
		private var _simplePrice:int;
		private var _simpleTime:int;
		private var _simpleXp:int;
		private var _multiNames:String;
		private var _multiQuantities:Array;
		private var _multiCosts:Array;
		private var _multiPrices:Array;
		private var _multiTimes:Array;
		private var _multiXps:Array;
		private var _upgradeConfig:Array;
		
		public var goodId:int;
		public var shopType:int;
		public var unlockCost:int;
		public var configType:String;
		public var maxLevel:int;
		
		public function GoodInfo(data:XML)
		{
			this._baseXml = data;
			this._level = 0;
			this.goodId = int(data.@id);
			this._symbolName = data.@symbol;
			this.shopType = int(data.@shopType);
			this.unlockCost = int(data.@unlockCost);
			if (data.@name.toString() != "")
			{
				this.configType = SIMPLE_TYPE;
				this._simpleName = data.@name;
				this._simpleQuantity = int(data.@quantityPerPack);
				this._simpleCost = int(data.@costPerPack);
				this._simplePrice = int(data.@defaultPrice);
				this._simpleTime = int(data.@timePerPack);
				this._simpleXp = int(data.@xpPerPack);
				this.maxLevel = 0;
			}
			else
			{
				this.configType = UPGRADE_TYPE;
				this._multiNames = JSON.decode(data.@names);
				this._multiQuantities = JSON.decode(data.@quantityPerPack);
				this._multiCosts = JSON.decode(data.@costPerPack);
				this._multiPrices = JSON.decode(data.@defaultPrice);
				this._multiTimes = JSON.decode(data.@timePerPack);
				this._multiXps = JSON.decode(data.@xpPerPack);
				if (data.@upgradeConfig.toString() != "")
				{
					this.createUpgradeConfig(JSON.decode(data.@upgradeConfig));
				}
				else
				{
					this._upgradeConfig = [];
				}
				this.maxLevel = this._multiCosts.length - 1;
			}
		}
		
		public function get timePerPack():int
		{
			if (this.configType == SIMPLE_TYPE)
			{
				return this._simpleTime;
			}
			return this._multiTimes[this._level];
		}
		
		public function get costPerPack():int
		{
			if (this.configType == SIMPLE_TYPE)
			{
				return this._simpleCost;
			}
			return this._multiCosts[this._level];
		}
		
		public function get defaultPrice():int
		{
			if (this.configType == SIMPLE_TYPE)
			{
				return this._simplePrice;
			}
			return this._multiPrices[this._level];
		}
		
		public function get symbolName():String
		{
			return (this._symbolName + "_" + this._level);
		}
		
		public function set level(_arg1:int):void
		{
			this._level = _arg1;
		}
		
		public function get level():int
		{
			return Math.min(this._level, this.maxLevel);
		}
		
		private function createUpgradeConfig(data:Array):void
		{
			var coinsList:Array;
			var coins:String;
			var num:int;
			this._upgradeConfig = new Array();
			for each (coinsList in data)
			{
				coins = coinsList[0];
				num = coinsList[1];
				this._upgradeConfig.push(new UpgradeInfo(coins, num));
			}
		}
	}
}